package meadproperty.dialog;

import meadproperty.service.Config;
import meadproperty.service.Constant;
import meadproperty.util.RunTimeUtil;
import meadproperty.views.PropertyViewer;

import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

public class ManageDialog extends TitleAreaDialog {
	List listKeySet;
	List listFileSet;

	public ManageDialog(Shell parentShell) {
		super(parentShell);
	}

	public static final int OPEN = 9999;

	public static final int DELETE = 9998;

	// String[] items = new String[] { "mead", "lai" };
	// String[] itemsToOpen;

	public void create() {
		super.create();
		setTitle("Mail");
		setMessage("You have mail! \n It could be vital for this evening...");
	}

	/**
	 * @see org.eclipse.jface.dialogs.Dialog#
	 *      createDialogArea(org.eclipse.swt.widgets.Composite) Here we fill the
	 *      center area of the dialog
	 */
	protected Control createDialogArea(Composite parent) {
		// Create new composite as container
		final Composite area = new Composite(parent, SWT.NULL);
		// We use a grid layout and set the size of the margins
		final GridLayout gridLayout = new GridLayout();
		gridLayout.marginWidth = 15;
		gridLayout.marginHeight = 10;
		area.setLayout(gridLayout);

		final GridData gridData = new GridData();
		gridData.widthHint = 200;

		this.listFileSet = new List(area, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
		for (String item : Constant.ACTIVE_CONFIG.getFileSet()) {
			this.listFileSet.add(item);
		}
		this.listFileSet.setLayoutData(gridData);
		this.listKeySet = new List(area, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
		for (String item : Constant.ACTIVE_CONFIG.getKeySet()) {
			this.listKeySet.add(item);
		}
		this.listKeySet.setLayoutData(gridData);

		return area;
	}

	private void validate() {
	}

	protected void createButtonsForButtonBar(Composite parent) {
		// Create Open button
		Button delKey = createButton(parent, OPEN, "del Key", true);
		// Initially deactivate it
		delKey.setEnabled(true);
		// Add a SelectionListener
		delKey.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				int keys[] = ManageDialog.this.listKeySet.getSelectionIndices();
				if (ManageDialog.this.listKeySet.getSelectionCount() == 1) {
					String item = ManageDialog.this.listKeySet.getItem(keys[0]);
					ManageDialog.this.listKeySet.remove(keys[0]);// del view
					Constant.ACTIVE_CONFIG.delKey(item);// del file
					setTitle("delete key# " + item);
				}
			}
		});
		Button deleteButton = createButton(parent, DELETE, "del Fiel", false);
		deleteButton.setEnabled(true);
		deleteButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				int files[] = ManageDialog.this.listFileSet
						.getSelectionIndices();
				if (ManageDialog.this.listFileSet.getSelectionCount() == 1) {
					String item = ManageDialog.this.listFileSet
							.getItem(files[0]);
					if (item.equals(Constant.VIEWER_KEY_LABEL)) {
						setTitle("can't delete Key Title");
						return;
					}
					ManageDialog.this.listFileSet.remove(files[0]);// del view
					Constant.ACTIVE_CONFIG.delFile(item);// del file
					setTitle("delete file# " + item);
				}
				int keys[] = ManageDialog.this.listFileSet
						.getSelectionIndices();
		
			}
		});
		// Create Cancel button
		Button cancelButton = createButton(parent, CANCEL, "Cancel", false);
		// Add a SelectionListener
		cancelButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				setReturnCode(CANCEL);
				Constant.ACTIVED_MARK=false;
				Constant.ACTIVE_CONFIG = new Config().fromXML();
				Constant.ACTIVED_MARK=true;
				RunTimeUtil.refreshProjects();
				PlatformUI.getWorkbench().getActiveWorkbenchWindow()
				.getActivePage().hideView(new PropertyViewer());
				close();
			}
		});
	}
}